//导入数据库模块
const db = require("../sql/index");
//加密插件
const bcrypt = require("bcryptjs");
// 用这个包来生成 Token 字符串
const jwt = require('jsonwebtoken')
// 导入配置文件
const config = require('../config')
// 后端开发经常会有使用http请求
const request = require('request')
// 文章的处理函数模块
const path = require('path');
// 又拍云
const upyuns = require('upyun');
const fs = require('fs');
const FormData = require('form-data');
const bucket = new upyuns.Bucket('sportqwe', '030306', 'MB3pbgMD5xoTYfV7hQFNMalvoNxNgdYb')
const upyun = new upyuns.Client(bucket);
const domain = 'http://sportqwe.test.upcdn.net/'
// 赛事
exports.game = (req, res) => {
    const sql = 'select * from game'
    db.query(sql, (err, results) => {
        if (err) return res.cc(err)
        res.send({
            status: 200,
            message: '获取赛事数据成功！',
            data: results,
        })
    })
}
// 新增赛事
exports.addgame = (req, res) => {
    console.log('req.body', req.body)
    if (!req.file || req.file.fieldname !== 'img') return res.cc('图片是必选参数！')
    fs.readFile(req.file.path, function (err, data) {
        // 上传成功
        upyun.putFile(req.file.filename, data).then(data => {
            console.log('上传成功', data);
            const articleInfo = {
                ...req.body,
                img: domain + req.file.filename,
            }
            const sql = `insert into game set ?`
            db.query(sql, articleInfo, (err, results) => {
                if (err) return res.cc(err)
                if (results.affectedRows !== 1) return res.cc('增加成功失败！')
                res.cc('增加成功！', 0)
            })
        }).catch(err => {
            // 上传失败
            console.log('又拍云上传失败', err);
        });
    })
    // const articleInfo = {
    //     ...req.body,
    //     img: '/uploads/' + req.file.filename,
    // }
    // const sql = `insert into game set ?`
    // db.query(sql, articleInfo, (err, results) => {
    //     if (err) return res.cc(err)
    //     if (results.affectedRows !== 1) return res.cc('增加成功失败！')
    //     res.cc('增加成功！', 0)
    // })
}
//搜索关键赛事
exports.checkgame = (req, res) => {
    var keys = Object.keys(req.body);
    console.log(keys);
    if (keys.includes("city")) {
        const sql = 'SELECT * FROM game WHERE city = ? '
        db.query(sql, [req.body.city], (err, results) => {
            if (err) return res.cc(err)
            res.send({
                status: 200,
                message: '获取赛事数据成功！',
                data: results,
            })
        })
    } else if (keys.includes("opentime")) {
        console.log("opentime", req.body.opentime)
        const sql = 'SELECT * FROM game WHERE date_format(opentime,"%Y%m") = date_format(?,"%Y%m") '
        db.query(sql, [req.body.opentime], (err, results) => {
            if (err) return res.cc(err)
            res.send({
                status: 200,
                message: '获取赛事数据成功！',
                data: results,
            })
        })
    } else if (keys.includes("state")) {
        const sql = 'SELECT * FROM game WHERE state = ? '
        db.query(sql, [req.body.state], (err, results) => {
            if (err) return res.cc(err)
            res.send({
                status: 200,
                message: '获取赛事数据成功！',
                data: results,
            })
        })
    }
}

//赛事详情增加
exports.addgamexq = (req, res) => {
    const sql2 = `select * from activity_item where id= ${req.body.obj_id}`
    db.query(sql2, (err, results) => {
        if (err) return res.cc(err)
        if (results.length == 1 && results[0].id == req.body.obj_id) return res.cc('obj_id被占用，请更换后重试！')
        if (results.length == 0) {
            if (!req.file || req.file.fieldname !== 'img') return res.cc('图片是必选参数！')
            fs.readFile(req.file.path, function (err, data) {
                // 上传成功
                upyun.putFile(req.file.filename, data).then(data => {
                    console.log('上传成功', data);
                    const articleInfo = {
                        ...req.body,
                        img: domain + req.file.filename
                    }
                    const sql = `insert into activity_obj set ?`
                    db.query(sql, articleInfo, (err, results) => {
                        if (err) return res.cc(err)
                        if (results.affectedRows !== 1) return res.cc('增加成功失败！')
                        res.cc('增加成功！', 0)
                    })
                }).catch(err => {
                    // 上传失败
                    console.log('又拍云上传失败', err);
                });
            })
        }
    })
}


